//每次组件发送请求时，自动携带token，主动打开loading，数据返回值，关闭loading

import axios from 'axios';
import Vue from 'vue';
import vm from '../main.js'

// 添加一个请求拦截
axios.interceptors.request.use(function (config) {
  
    
   
   let user = window.localStorage.getItem('user');//{err:0,msg:xx,data,token}
   let token = user ? JSON.parse(user).token : '';
   
   // config.headers={token:'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImhlaGVoZTIxMDUiLCJfaWQiOiI2MTE2NjE4ZTU2ZjU3MmFjN2Q1OGIxNmIiLCJpYXQiOjE2Mjk5NDYxMzEsImV4cCI6MTYzMDAzMjUzMX0.0eFymqdslmqnzeMA32h3sCoeWyIsXAQg2KXicMt36F8'}
   config.headers={token}
    
    // 主动打开loading，
	
	vm.bLoading = true;
    
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// 添加一个响应拦截
axios.interceptors.response.use(function (response) {
	
    //关闭loading
	 vm.bLoading = false;
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });

Vue.prototype.$axios = axios;


export default axios;